package com.yaowk.system.server.controller;

import com.yaowk.common.api.model.result.Transfer;
import com.yaowk.common.api.model.result.Tree;
import com.yaowk.security.utils.ISecurityUtils;
import com.yaowk.system.api.MenuApi;
import com.yaowk.system.api.model.Menu;
import com.yaowk.system.api.model.Resource;
import com.yaowk.system.server.service.IMenuService;
import com.yaowk.system.server.service.IResourceService;
import com.yaowk.system.server.utils.MenuUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

@RestController
@Api(tags = "菜单")
public class MenuController implements MenuApi {
    @Autowired
    private IMenuService menuService;
    @Autowired
    private IResourceService resourceService;
    @Autowired
    private ISecurityUtils securityUtils;

    @Override
    @ApiOperation("菜单树")
    public Tree<Menu, Long> list() {
        List<Menu> menus = menuService.findAll();
        Tree<Menu, Long> tree = new Tree<>();
        Menu menu = menuService.findById(1l);
        tree.setValue(menu);
        MenuUtil.findMenuChildren(menus, tree);
        return tree;
    }

    @Override
    @ApiOperation("菜单详情")
    public Menu findOne(@PathVariable Long id) {
        return menuService.findById(id);
    }

    @Override
    @ApiOperation("保存菜单")
    public Menu save(Menu menu) {
        return menuService.save(menu);
    }

    @Override
    @ApiOperation("删除菜单")
    public void delete(@PathVariable Long id) {
        menuService.deleteById(id);
    }


    @Override
    @ApiOperation("菜单资源列表")
    public Transfer<Resource, Long> menuResource(@PathVariable Long menuId) {
        List<Resource> resourceList = resourceService.findAll();
        List<Resource> menuResourceList = resourceService.findAllByMenuId(menuId);

        // 封装穿梭框对象
        Transfer<Resource, Long> transfer = new Transfer<>();
        Set<Long> checkedKeys = transfer.getCheckedKeys();
        // 已选中的key
        for (Resource resource : menuResourceList) {
            checkedKeys.add(resource.getId());
        }

        Set<Transfer<Resource, Long>.TransferData> data = transfer.getData();
        for (Resource resource : resourceList) {
            Transfer<Resource, Long>.TransferData transferData = transfer.new TransferData();
            transferData.setKey(resource.getId());
            transferData.setValue(resource);
            transferData.setLabel(resource.getName());
            data.add(transferData);
        }
        return transfer;
    }

    @Override
    @ApiOperation("编辑菜单资源关系")
    public void editMenuResource(@PathVariable Long menuId, @RequestParam(required = false) List<Long> resourceId) {
        menuService.editResource(menuId, resourceId);
    }


}
